#import "LauncherViewTestController.h"
//#import "UINavigationBar+CustomImage.h"

@implementation LauncherViewTestController

- (void)dealloc
{
	[super dealloc];
}

- (id)init
{
  if (self = [super init])
  {
    self.title = @"Stud.IP";
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(addDarthVader)
                                                 name:@"addDarthVader" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(addDesigningUserExperiences)
                                                 name:@"addDesigningUserExperiences" object:nil];
  }
	
  return self;
}

- (void)viewWillAppear:(BOOL)animated
{
  NSLog(@"will appear.");
  [super viewWillAppear:animated];
  
//  [self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCourse:)] autorelease] animated:YES];
  
  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"woodNavibarGlow"] withTag:123];
  
  
	self.navigationBarTintColor = [UIColor colorWithRed:0.514 green:0.427 blue:0.306 alpha:1.000]; 
  
  
  
}

- (void)viewWillDisappear:(BOOL)animated
{
  NSLog(@"will disappear.");
  [super viewWillDisappear:animated];
  
  [self.navigationController.navigationBar removeBackgroundImage:123];
	self.navigationBarTintColor = nil;  
}



- (void)loadView
{
	[super loadView];
	
	//[self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCourse:)] autorelease] animated:YES];
  //  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"woodNavibarGlow"] withTag:123];
	self.navigationBarTintColor = [UIColor colorWithRed:0.514 green:0.427 blue:0.306 alpha:1.000];
	// TODO: Dokumentieren!
	self.statusBarStyle = UIStatusBarStyleBlackOpaque;
	
	NSArray *launcherItems = [NSArray arrayWithObjects:
                            [[[TTLauncherItem alloc] initWithTitle:@"Neuigkeiten"
                                                             image:@"bundle://neuigkeiten.png"
                                                               URL:@"fb://news" canDelete:NO] autorelease],
                            [[[TTLauncherItem alloc] initWithTitle:@"Kurse"
                                                             image:@"bundle://kurse.png"
                                                               URL:@"fb://courses" canDelete:NO] autorelease],
                            [[[TTLauncherItem alloc] initWithTitle:@"Kalender"
                                                             image:@"bundle://kalender.png"
                                                               URL:@"fb://item4" canDelete:NO] autorelease],
                            [[[TTLauncherItem alloc] initWithTitle:@"Post"
                                                             image:@"bundle://post.png"
                                                               URL:@"fb://inbox" canDelete:NO] autorelease],
                            nil];
	
  _launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
  //_launcherView.backgroundColor = [UIColor blackColor];
  _launcherView.delegate = self;
  _launcherView.columnCount = 4;
  _launcherView.pages = [NSArray arrayWithObjects:
                         launcherItems, nil
                         ];
	
	[self.view addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wood_dark_shadow.png"]] autorelease]];
	
  [self.view addSubview:_launcherView];
	
	for (TTLauncherItem *launcherItem in launcherItems)
	{
		launcherItem.style = @"launcherButtonStyle";
	}
}

- (void)launcherView:(TTLauncherView*)launcher didSelectItem:(TTLauncherItem*)item
{
	TTOpenURL([item URL]);
}

- (void)launcherViewDidBeginEditing:(TTLauncherView*)launcher
{
  [self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] 
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                               target:_launcherView action:@selector(endEditing)] autorelease] animated:YES];
}

- (void)launcherViewDidEndEditing:(TTLauncherView*)launcher
{
  [self.navigationItem setRightBarButtonItem:nil animated:YES];
}

- (IBAction)addCourse:(id)sender
{
	TTOpenURL(@"fb://item4");
}

- (void)addDesigningUserExperiences
{
  NSMutableArray *launcherItems = [[_launcherView.pages objectAtIndex:0] mutableCopy];
  
  [launcherItems addObject:[[[TTLauncherItem alloc] initWithTitle:@"Designing user experiences"
                                   image:@"bundle://kurs.png"
                                                              URL:@"fb://courseDetails/1" canDelete:YES] autorelease]];
  _launcherView.pages = [NSArray arrayWithObjects:
                         launcherItems, nil
                         ];
}

- (void)addDarthVader
{
  NSMutableArray *launcherItems = [[_launcherView.pages objectAtIndex:0] mutableCopy];
  
  [launcherItems addObject:[[[TTLauncherItem alloc] initWithTitle:@"Darth Vader"
                                                            image:@"bundle://person.png"
                                                              URL:@"fb://inboxDetails" canDelete:YES] autorelease]];
  _launcherView.pages = [NSArray arrayWithObjects:
                         launcherItems, nil
                         ];
}

@end
